package top.lingkang.finalgateway.admin.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Assert;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.core.handle.ModelAndView;
import top.lingkang.finalgateway.FinalGateway;
import top.lingkang.finalgateway.admin.entity.ConfigHistoryEntity;
import top.lingkang.finalgateway.admin.mapper.ConfigHistoryMapper;
import top.lingkang.finalgateway.config.RouterConfig;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lingkang
 * Created by 2024/4/27
 */
@Slf4j
@Controller
public class WebController {
    @Inject("${admin.password}")
    private String password;
    @Inject
    private ConfigHistoryMapper configHistoryMapper;

    public static String index = null;

    @Mapping("/")
    public Object index(Context context) {
        context.contentType("text/html;charset=utf-8");
        if (index == null) {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/index.html");
            if (in != null)
                index = IoUtil.readUtf8(in);
            else
                index = "404";
        }
        return index;
    }

    @Mapping(value = "/login", method = MethodType.GET)
    public Object login() {
        return new ModelAndView("login.ftl");
    }

    // ---------------------------------------------------------------------------------------------------
    @Mapping(value = "/console", method = MethodType.GET)
    public Object console() {
        int runNumber = 0;
        for (RouterConfig config : FinalGateway.config.getRouterConfigs()) {
            if (FinalGateway.service.containsKey(config.getPort()))
                runNumber++;
        }
        return new ModelAndView("view/console.ftl")
                .put("runNumber", runNumber)
                .put("totalNumber", FinalGateway.config.getRouterConfigs().size());
    }

    @Mapping(value = "/config", method = MethodType.GET)
    public Object config() {
        return new ModelAndView("view/config.ftl").put("xml", FinalGateway.config.getXml());
    }

    @Mapping(value = "/actionLog", method = MethodType.GET)
    public Object actionLog() {
        return new ModelAndView("view/log.ftl");
    }

    @Mapping(value = "/history", method = MethodType.GET)
    public Object history() {
        return new ModelAndView("view/history.ftl");
    }

    @Mapping(value = "/xmldiff", method = MethodType.GET)
    public Object xmldiff(String id) {
        Assert.notBlank(id, "id 不能为空");
        ConfigHistoryEntity entity = configHistoryMapper.selectById(id);
        Assert.notNull(entity, "配置不存在");
        return new ModelAndView("window/xmldiff.ftl")
                .put("id", entity.getId())
                .put("old", entity.getOld())
                .put("time", entity.getCreateTime())
                .put("new", entity.getXml());
    }

    @Mapping(value = "/router", method = MethodType.GET)
    public Object router() {
        List<RouterConfig> configs = FinalGateway.config.getRouterConfigs();
        List<Integer> ports = new ArrayList<>();
        configs.forEach(r -> ports.add(r.getPort()));
        return new ModelAndView("window/router.ftl")
                .put("ports", ports)
                .put("configs", configs);
    }

    @Mapping(value = "/sample", method = MethodType.GET)
    public void sample(Context context) {
        String sample = IoUtil.readUtf8(getClass().getClassLoader().getResourceAsStream("config-sample.xml"));
        context.headerSet("Content-Type", "application/xml");
        context.output(sample);
    }

}
